<?php
/**
 * @author Inhere
 * @version v1.0
 * Use : 缓存管理服务
 * Date : 2015-1-10
 * CacheServiceProvider.php
 */
namespace ulue\base\providers;

use Ioc;
use ulue\core\ioc\Container;
use ulue\parts\cache\CacheFactory;

class CacheServiceProvider extends AbstractConfigurableServiceProvider
{
    /**
     * 注册一个服务提供容器， CacheServiceProvider
     * @param  Container $container 传入容器
     * @return void [type]               [description]
     */
    public function register(Container $container)
    {
        $config = $this->options;

        $closure = function($driver=null, $dataHandler=null, array $options=[]) use ($config)
        {
            $options = array_merge($config, $options);

            $options['dataHandler'] = isset($options['dataHandler']) ? $options['dataHandler'] : null;
            $options['driver'] = isset($options['driver']) ? $options['driver'] : null;

            if (!isset($options['debug'])) {
                //$options['debug'] = (bool) $config->get('cache.debug');
                $options['debug'] = APP_DEBUG;
            }

            $dataHandler = $dataHandler ?: $options['dataHandler'];// $dataHandler===false
            $driver      = $driver ?:$options['driver'];

            return CacheFactory::getCache($driver, $dataHandler, $options);
        };

        $container->set('cache',$closure)->alias('app.cache')->alias('application.cache');

    }
}